#include "opencvhelper.h"
#include "processwin.h"
#include "ui_processwin.h"
//#include "glimagewidget.h"
#include <QDockWidget>
#include <QString>
#include <debugx>
#include "MainWinUtil.h"
#include<QWindow>

QDockWidget *dockProcess=0;
ProcessWin *processWin=0;

extern Plugin *plugin;
extern std::string filePathForOpencv;
extern bool isZip;

extern QHash<QString,ProcessFun *> processFunMap;
using namespace cv;

void getDockProcess()
{
    if(dockProcess)return;
    processWin=new ProcessWin(plugin);
    auto lvFun=processWin->ui->lvFun;
    QVector<QString> keys;
    foreach (auto key, processFunMap.keys())
        keys.append(key);
    std::sort(keys.begin(),keys.end());
    foreach (auto key, keys)
    {
        auto item=new QListWidgetItem(key);
        auto fun=processFunMap.value(key);
        item->setToolTip(fun->description);
        item->setData(Qt::UserRole,QString::fromStdString(fun->defaultArg));
        item->setData(Qt::UserRole+1,QString::fromStdString(fun->defaultArg));
        lvFun->addItem(item);
    }
    dockProcess = createDockWidget(plugin,processWin);
    //dockProcess->setStyleSheet(getStyleSheet());
    processWin->setStyleSheet(getStyleSheet());

}
void exeProcess(QListWidgetItem *item)
{
    auto img= processWin->imageWin->getImage();
    Mat src;
    if(img.isNull())
    {
        if(filePathForOpencv.empty())return;
        src = imread(filePathForOpencv.data());
    }else
        src=QImage2cvMat(img);

    Mat input,output;
    auto text=item->text();
    auto funDesc=processFunMap.value(text);
    auto fun=funDesc->fun;
    auto arg=item->data(Qt::UserRole+1).toString().toStdString();
    input=src.clone();
    fun(input,output,src,arg);
    img=cvMat2QImage(output);
    processWin->resultWin->setImage(img);
}

void exeProcess(Mat &src,cv::Mat &output)
{
    if(!processWin)
    {
        output=src;
        return;
    }
    auto lvSelected=processWin->ui->lvSelected;
    int count=lvSelected->count();
    if(count==0)
    {
        output=src;
        return;
    }
    Mat input;
    input=src.clone();
    for(int i=0;i<count;i++)
    {
        auto item=lvSelected->item(i);
        auto text=item->text();
        auto funDesc=processFunMap.value(text);
        auto fun=funDesc->fun;
        auto arg=item->data(Qt::UserRole+1).toString().toStdString();
        fun(input,output,src,arg);
        input=output;
    }

}
void exeProcess(Mat &src)
{
    cv::Mat output;
    exeProcess(src,output);
    auto img=cvMat2QImage(output);
    processWin->resultWin->setImage(img);
}
void exeProcess()
{
   auto img= processWin->imageWin->getImage();
    Mat src;
    if(img.isNull())
   {
        if(filePathForOpencv.empty())return;
        src = imread(filePathForOpencv.data());
   }else
        src=QImage2cvMat(img);
    exeProcess(src);
}
void showImageProcess()
{
    debugx();
    if(!processWin)return;
    cv::Mat src = cv::imread(filePathForOpencv.data());
    if (src.empty())return;
    auto img=cvMat2QImage(src);
    processWin->imageWin->setImage(img);
    exeProcess(src);
}



